#!/bin/bash
#Author     :xiaokai
#Mail       :xiaokai.c@foxmail.com
#Date       :2020/08/15
#Description:auto mount nfs

host=192.168.1.100
localMountDir="/home/debian"

#Extract the path shared by the server
mountPath=$(showmount -e $host|awk  'BEGIN{a=0}NR>1{m[a]=$1;a++}END{for(i in m){print m[i]}}')

if [[ $? != 0 ]] ;then
	exit 1
fi

mountPathArray=()
mountDirName=()

for i in $mountPath;do
	mountPathArray[${#mountPathArray[*]}]=${i}
done

for i in ${!mountPathArray[*]};do
	mountDirName[$i]=${mountPathArray[$i]##*/}
done

for i in ${!mountPathArray[*]};do
	if [ ! \( -d "$localMountDir/${mountDirName[$i]}" \) ];then
		mkdir $localMountDir/${mountDirName[$i]}
	fi
	mount -t nfs $host:${mountPathArray[$i]} $localMountDir/${mountDirName[$i]}
done
